package com.dd.shop4j.console.goods.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.dd.shop4j.common.utils.Constant;
import com.dd.shop4j.common.utils.ExcelUtils;
import com.dd.shop4j.common.utils.Message;
import com.dd.shop4j.common.utils.Message.MessageType;
import com.dd.shop4j.console.goods.controller.forms.GoodForm;
import com.dd.shop4j.console.goods.controller.forms.GoodsQueryForm;
import com.dd.shop4j.console.goods.service.GoodsAttrManagerService;
import com.dd.shop4j.console.goods.service.GoodsBrandManagerService;
import com.dd.shop4j.console.goods.service.GoodsInfoManagerService;
import com.dd.shop4j.goods.enums.GoodStatus;
import com.dd.shop4j.goods.model.GoodsAttrDetails;
import com.dd.shop4j.goods.model.GoodsImage;
import com.dd.shop4j.goods.model.GoodsInfo;
import com.dd.shop4j.sys.service.SysConstantService;

@Controller
@RequestMapping("/admin/goods")
public class GoodsInfoController {

	@Resource
	private GoodsInfoManagerService goodsInfoManagerService;
	
	@Resource
	private GoodsBrandManagerService goodsBrandManagerService;
	
	@Resource
	private GoodsAttrManagerService goodsAttrManagerService;
	
	@Resource
	private SysConstantService sysConstantService;
	
	@RequestMapping("/list")
	public ModelAndView list(GoodsQueryForm query, Integer curpage, Integer pagesize) {
		if (curpage == null || curpage <= 0) {
			curpage = 1;
		}
		if (pagesize == null || pagesize <= 0) {
			pagesize = sysConstantService.getInteger(Constant.PAGE_SIZE_CODE);
		}
		ModelAndView view = new ModelAndView("/admin/goods/goods_list");
		view.addObject("query", query);
		view.addObject("page", goodsInfoManagerService.queryGoodsByPage(query, curpage, pagesize));
		return view;
	}
	
	@RequestMapping("/export")
	public void export(HttpServletResponse response) throws Exception {
		String codedFileName = java.net.URLEncoder.encode("商品导出列表", "UTF-8");  
        response.setHeader("content-disposition", "attachment;filename=" + codedFileName + ".xls");
        List<GoodsInfo> goodsInfos = goodsInfoManagerService.queryGoodsInfos(null);
        List<List<Object>> data = ExcelUtils.getExcelData(goodsInfos);
		ExcelUtils.writeExcel(response.getOutputStream(), data);
	}
	
	@ResponseBody
	@RequestMapping("/list/updatenbh")
	public void updateNBHStatus(String type, Integer goodId, boolean flag) {
		GoodsInfo good = new GoodsInfo();
		good.setId(goodId);
		if ("new".equals(type)) {
			good.setNewFlag(flag);
		} else if ("best".equals(type)) {
			good.setBestFlag(flag);
		} else if ("hot".equals(type)) {
			good.setHotFlag(flag);
		} else {
			return;
		}
		goodsInfoManagerService.updateSelectiveByPrimaryKey(good);
	}

	@RequestMapping(value = "/insert", method = RequestMethod.GET)
	public ModelAndView insert() {
		ModelAndView view = new ModelAndView("/admin/goods/goods_insert");
		view.addObject("brands", goodsBrandManagerService.getAll());
		view.addObject("attrs", goodsAttrManagerService.getAllGoodsAttrs());
		return view;
	}
	
	@RequestMapping(value = "/insert", method = RequestMethod.POST)
	public ModelAndView insert(GoodForm goodForm) {
		goodsInfoManagerService.insertGood(goodForm);
		Message message = new Message(MessageType.CONSOLE_SUCCESS);
		message.setGotoPage("/admin/goods/insert");
		message.setContext("恭喜，商品保存成功。");
		return message.getView();
	}
	
	@ResponseBody
	@RequestMapping("/attrDetails/{attrId}")
	public List<GoodsAttrDetails> attrDetails(@PathVariable("attrId")Integer attrId) {
		return goodsAttrManagerService.getAttrDetailsByAttrId(attrId);
	}

	@ResponseBody
	@RequestMapping("/updateImageTitle")
	public void updateImageTitle(GoodsImage image) {
		goodsInfoManagerService.updateGoodsImage(image);
	}
	
	@RequestMapping("/deleteImage/{imageId}")
	public void deleteImage(@PathVariable("imageId") Integer imageId) {
		goodsInfoManagerService.deleteGoodsImageByImageId(imageId);
	}
	
	@RequestMapping(value = "/update/{goodId}", method = RequestMethod.GET)
	public ModelAndView update(@PathVariable("goodId") Integer goodId) {
		ModelAndView view = new ModelAndView("/admin/goods/goods_update");
		GoodsInfo good = goodsInfoManagerService.loadGoodById(goodId);
		view.addObject("good", good);
		view.addObject("brands", goodsBrandManagerService.getAll());
		view.addObject("attrs", goodsAttrManagerService.getAllGoodsAttrs());
		view.addObject("attrDetails", goodsAttrManagerService.getAttrDetailsByAttrId(good.getAttrId()));
		view.addObject("goodAttrDetails", goodsAttrManagerService.getGoodsInfoAttrDetailsByGoodId(goodId));
		view.addObject("description", goodsInfoManagerService.loadGoodDescriptionByGoodId(goodId));
		view.addObject("images", goodsInfoManagerService.getGoodsImagesByGoodId(goodId));
		return view;
	}
	
	@RequestMapping(value = "/update", method = RequestMethod.POST)
	public ModelAndView update(GoodForm form) {
		goodsInfoManagerService.updateGood(form);
		Message message = new Message(MessageType.CONSOLE_SUCCESS);
		message.setContext("恭喜，商品保存成功。");
		message.setGotoPage("/admin/goods/update/" + form.getGood().getId());
		return message.getView();
	}
	
	@RequestMapping("/delete/{goodId}")
	public ModelAndView delete(@PathVariable("goodId") Integer goodId) {
		//这里删除只是把商品移入到回收站
		goodsInfoManagerService.updateGoodsInfoStatus(goodId, GoodStatus.DELETE);
		Message message = new Message(MessageType.CONSOLE_SUCCESS);
		message.setContext("恭喜，商品删除成功，你可以在回收站恢复。");
		message.setGotoPage("/admin/goods/list");
		return message.getView();
	}
}
